package pl.org.lutra.planner.gui.util;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Window;

import javax.swing.BorderFactory;
import javax.swing.JComponent;

import pl.org.lutra.planner.gui.resources.Text;

public class GuiUtil {
    public static void center(Window window, Window parent) {
        Rectangle parentBounds;
        
        if (parent != null) {
            parentBounds = parent.getBounds();
        } else {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            parentBounds = gd.getDefaultConfiguration().getBounds();
        }
        
        int x = (int) (parentBounds.getCenterX() - (window.getWidth() / 2));
        int y = (int) (parentBounds.getCenterY() - (window.getHeight() / 2));
        
        window.setLocation(x, y);
    }
    
    public static void setTitleBorderWithPadding(JComponent component, String titleKey) {
        component.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder(Text.get(titleKey)),
                BorderFactory.createEmptyBorder(10, 0, 5, 0)));
    }
    
    public static String prepareMultiLineToolTip(String string) {
        StringBuffer sb = new StringBuffer();
        
        sb.append("<html>");
        
        String[] split = string.split("\n");
        sb.append(split[0]);
        
        for (int i = 1; i < split.length; i++) {
            sb.append("<br/>").append(split[i]);
        }
        
        sb.append("</html>");
        
        return sb.toString();
    }
}
